﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using BestBuy.DTO;
using BestBuy.Common;
namespace BestBuy.PageUser
{
    public partial class MbSellling : System.Web.UI.Page
    {
        private static List<DTOProduct> liproduct = new List<DTOProduct>();
        private int[] masp;
        protected void Page_Load(object sender, EventArgs e)
        {
            checkTonTai();
            if (!IsPostBack)
            {
                loadInfoPresent();
                loadListProduct();
            }            
        }
        private void checkTonTai()
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }
        private void loadInfoPresent()
        {
            int idUser = int.Parse(Session["IDDN"].ToString());
            int idcongty = cManageProduct.getIdCompany(idUser);
            getCountProduct.Text = cManageProduct.getCountProduct(idUser).ToString();
            getNameCompany.Text = cManageProduct.getNameCompany(idcongty).ToString();
        }
        private void checkVerify()
        {

        }
        private void loadListProduct()
        {
            liproduct = new List<DTOProduct>();
            List<SanPham> li = cManageProduct.getListProduct(int.Parse(Session["IDDN"].ToString()));
            foreach (var item in li)
            {
                DTOProduct dt = new DTOProduct();
                dt.IdProduct = item.IDSanPham;
                dt.Title = item.TenSanPham;
                dt.DisPlayorder = 1;
                dt.Datepost = item.NgayDang;
                dt.Status = Convert.ToInt32(item.TinhTrang);
                dt.Image = item.HinhAnh;                
                liproduct.Add(dt);
            }
            gvListProduct.DataSource = liproduct;
            gvListProduct.DataBind();
        }

        protected void gvListProduct_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //kiem tra unverified
                int status = (int)DataBinder.Eval(e.Row.DataItem, "Status");
                int IDSanPham = (int)DataBinder.Eval(e.Row.DataItem, "IdProduct");
                HyperLink copy = e.Row.FindControl("HyperLink4") as HyperLink;
                if (copy != null)
                {
                    copy.Visible = true;
                    copy.NavigateUrl = "CopyProduct.aspx?IdProduct=" + IDSanPham;
                }
                switch (status)
                {
                    case (int)SanPhamStatus.Active:
                        HyperLink link = e.Row.FindControl("HyperLink3") as HyperLink;
                        
                        if (link != null)
                        {
                            link.Visible = true;
                            link.NavigateUrl = "EditProduct.aspx?IdProduct=" + IDSanPham;
                        }
                        break;
                    case (int)SanPhamStatus.Unverified:
                        Literal lbUnverified = e.Row.FindControl("lbUnverified") as Literal;
                        if (lbUnverified != null)
                        {
                            lbUnverified.Visible = true;
                        }
                        break;
                }
                //kiem tra co hinh anh kg
                string checkimage = (string)DataBinder.Eval(e.Row.DataItem, "Image");            
                if (checkimage != "")
                {
                    Literal lita = e.Row.FindControl("Literal1") as Literal;
                    HyperLink hyper = e.Row.FindControl("HyperLink2") as HyperLink;
                    if (lita !=null && hyper !=null)
                    {                    
                        lita.Text = "1";
                        hyper.Visible = true;
                        hyper.NavigateUrl = "ImageProduct.aspx?IdProduct=" + IDSanPham;
                    }
                }
                else
                {
                    Literal lita = e.Row.FindControl("Literal1") as Literal;
                    HyperLink hyper = e.Row.FindControl("HyperLink2") as HyperLink;
                    if (lita != null && hyper != null)
                    {
                        lita.Text = "0";
                        hyper.Visible = false;
                    }
                }           
            }                        
        }

        
        protected void Btremove_Click(object sender, EventArgs e)
        {
            for (int i =0;i<gvListProduct.Rows.Count;i++)
            {
                CheckBox cb = (CheckBox)gvListProduct.Rows[i].FindControl("chkXoa");
                if (cb.Checked)
                {
                    foreach (var ite in liproduct)
                    {
                        if (ite.IdProduct == Convert.ToInt32(((Label)gvListProduct.Rows[i].FindControl("lbIdSanPham")).Text))
                        {
                            ite.Check = true;
                        }
                    }
                }                
            }
            foreach (var item in liproduct)
            {
                if (item.Check)
                {
                    cManageProduct.deleteProduct(item.IdProduct);
                }
            }
            loadListProduct();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            gvListProduct.DataSource = null;
            gvListProduct.DataBind();
            if (Txtsearch.Text != "")
            {
                liproduct = new List<DTOProduct>();
                List<SanPham> li = cManageProduct.searchProduct(int.Parse(Session["IDDN"].ToString()), Txtsearch.Text);
                foreach (var item in li)
                {
                    DTOProduct dt = new DTOProduct();
                    dt.IdProduct = item.IDSanPham;
                    dt.Title = item.TenSanPham;
                    dt.DisPlayorder = 1;
                    dt.Datepost = item.NgayDang;
                    dt.Status = item.TinhTrang.Value;
                    dt.Image = item.HinhAnh;
                    liproduct.Add(dt);
                }
                gvListProduct.DataSource = liproduct;
                gvListProduct.DataBind();
            }
            else
            {
                loadListProduct();
            }
        }
        protected void Display_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvListProduct.Rows.Count; i++)
            {
                TextBox tb = (TextBox)gvListProduct.Rows[i].FindControl("txtdisplay");
                foreach (var ite in liproduct)
                {
                    if (ite.IdProduct == Convert.ToInt32(((Label)gvListProduct.Rows[i].FindControl("lbIdSanPham")).Text))
                    {
                        try
                        {
                            ite.DisPlayorder = int.Parse(tb.Text);
                        }
                        catch (System.Exception ex)
                        {
                            ite.DisPlayorder = 1;
                        }                        
                    }
                }
            }
            sorting();
        }
        private void sorting()
        {
            for (int i = 0; i < liproduct.Count; i++)
            {
                for (int j = i + 1; j <= liproduct.Count - 1; j++)
                {
                    if (liproduct[i].DisPlayorder > liproduct[j].DisPlayorder)
                    {
                        DTOProduct temp = liproduct[i];
                        liproduct[i] = liproduct[j];
                        liproduct[j] = temp;
                    }
                }
            }
            gvListProduct.DataSource = null;
            gvListProduct.DataBind();
            gvListProduct.DataSource = liproduct;
            gvListProduct.DataBind();
        }      
    }
}